package mx.itesm.twritter.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import mx.itesm.twritter.shared.Tweet;
import mx.itesm.twritter.shared.ToDraw;
import mx.itesm.twritter.shared.map.Node;

/**
 * Class that interfaces java methods with javascript methods, specially to handle map and
 * twitter events :)
 * @author usuario
 *
 */
public class MapLayer {
	
	/** remote service **/
	private static final ParsingServiceAsync greetingService = GWT
			.create(ParsingService.class);
	public static Twritter twitterConsole;
		
	public static void requestParse(Tweet tweet) {
		
		greetingService.parse(tweet, new AsyncCallback<ToDraw>() {
			
			@Override
			public void onSuccess(ToDraw result) {
				drawPoly(result.getRoute(), result.getVelocity(), result.getAutor(), result.getText(), result.getHora());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Hubo un error al intenter contactar al servidor. " +
						"Por favor revisa tu conexion a internet");
			}
		});
		
	}
	
	public static void copyToBox(Tweet tweet) {
		twitterConsole.TweetText.setText(tweet.getText());
	}
	
	public static native void toTweet(JavaScriptObject tweet) /*-{
		var javaTweet = @mx.itesm.twritter.shared.Tweet::new()();
		
		javaTweet.@mx.itesm.twritter.shared.Tweet::setId(Ljava/lang/String;)(tweet.id);
		javaTweet.@mx.itesm.twritter.shared.Tweet::setCreatedAt(Ljava/lang/String;)(tweet.created_at)
		javaTweet.@mx.itesm.twritter.shared.Tweet::setText(Ljava/lang/String;)(tweet.tweet);
		javaTweet.@mx.itesm.twritter.shared.Tweet::setUser(Ljava/lang/String;)(tweet.user);
		
		@mx.itesm.twritter.client.MapLayer::requestParse(Lmx/itesm/twritter/shared/Tweet;)(javaTweet);
	}-*/;
	
	public static native void copyTweet(JavaScriptObject tweet) /*-{
		var javaTweet = @mx.itesm.twritter.shared.Tweet::new()();
		
		javaTweet.@mx.itesm.twritter.shared.Tweet::setId(Ljava/lang/String;)(tweet.id);
		javaTweet.@mx.itesm.twritter.shared.Tweet::setCreatedAt(Ljava/lang/String;)(tweet.created_at)
		javaTweet.@mx.itesm.twritter.shared.Tweet::setText(Ljava/lang/String;)(tweet.tweet);
		javaTweet.@mx.itesm.twritter.shared.Tweet::setUser(Ljava/lang/String;)(tweet.user);
		
		@mx.itesm.twritter.client.MapLayer::copyToBox(Lmx/itesm/twritter/shared/Tweet;)(javaTweet);
	}-*/;
		
	public static native void exportMethods() /*-{
		$wnd.callRPC   = $entry(@mx.itesm.twritter.client.MapLayer::toTweet(Lcom/google/gwt/core/client/JavaScriptObject;));
		$wnd.copyTweet = $entry(@mx.itesm.twritter.client.MapLayer::copyTweet(Lcom/google/gwt/core/client/JavaScriptObject;));
	}-*/;
	
	/**
	 * Draws a polygon on the map
	 * @param nodes
	 */
	public static native void drawPoly(List<Node> nodes, Integer velocity, String autor, String texto, String hora) /*-{
		var leng = nodes.@java.util.List::size()();
		
		var color = "#0033FF";
		
		var latlngs = new Array();
		
		if(velocity >= 0 && velocity <= 20){
			color = "#FF0000";//RED
		} else if(velocity >= 21 && velocity <= 40){
			color = "#FFEF00";//YELLOW
		} else if(velocity >= 40){
			color = "#21B600";//GREEN
		}
		
		for(var i=0; i<leng; i++) {
			var node = nodes.@java.util.List::get(I)(i);
			var lat = node.@mx.itesm.twritter.shared.map.Node::latitude;
			var lon = node.@mx.itesm.twritter.shared.map.Node::longitude;
			latlngs[i] = $wnd.toLatLng(lat, lon);
		}
		$wnd.drawPolygon(latlngs, color, autor, texto, hora);
	}-*/;
	
	
	/**
	 * Draws a marker with position node on the map
	 * @param node
	 */
	public static native void drawMyPosition(Node node) /*-{
		var lat = node.@mx.itesm.twritter.shared.map.Node::latitude;
		var lon = node.@mx.itesm.twritter.shared.map.Node::longitude;
		latlng =$wnd.toLatLng(lat,lon);
		$wnd.drawMarkerAtMyPosition(latlng);
	}-*/;
	
}
